var o = require("../UISoundManager.js"), i = require("../UIPopupBase.js");
cc.Class({
    extends: i,
    properties: {
        createPrefab: {
            default: null,
            type: cc.Prefab
        }
    },
    init: function () {
    },
    onLoad: function () {
        Global.notificationCenter.on(Global.eventName.MSG_HAPPYBAG5, this.bindData.bind(this), this),
            Global.notificationCenter.on(Global.eventName.MSG_REWARD_INFO, this.setData.bind(this), this),
            Global.msgHandler.requestTaskData(), this.activity1Content = Global.utils.searchNode(this.node, "activity1_content"),
            this.activity2Content = Global.utils.searchNode(this.node, "activity2_content"),
            this.activity3Content = Global.utils.searchNode(this.node, "activity3_content"),
            this.targetClickData = {}, this.defaultPlayMode = Global.config.playModel.XUELIU,
            this.defaultGameId = Global.config.GAMEID_NORMAL, Global.notificationCenter.emit(Global.eventName.MSG_SET_UNDO_RECEIVE, !0);
    },
    setData: function (e) {
        if ("recieve" == e.action) if (e.tip) Global.utils.showNodeCommon(e.tip, 2); else {
            var t = function () {
                var e = this.defaultPlayMode, t = Global.roomInfoModel.getRoomId();
                Global.msgHandler.requestQuickStart(e, t);
            }.bind(this), n = {
                reward: e.deltaChip,
                name: "金币",
                type: "freegive"
            };
            Global.UIPopupManager.addPopup("PopupPanel/Panel_shareGroup_Reward", function (e) {
                e.getComponent("shareGroup_RewardPanel").setData(n, t), Global.spriteAtlas.setSpriteFrame(e.getComponent("shareGroup_RewardPanel").rewardSp, "Texture/Rewards", "WP_3");
            });
        }
    },
    bindData: function (e) {
        if (Global.utils.hideWaitting(), e && "list" == e.action) this._tasks = e.tasks; else if (e && "getreward" == e.action) {
            var t = {};
            if (!(e.todotasks && e.todotasks.params && e.todotasks.params.rewards)) return;
            t = e.todotasks.params.rewards[0];
            var n = function () {
                Global.msgHandler.requestTaskData();
            };
            cc.loader.load({
                url: t.pic,
                type: "png"
            }, function (e, o) {
                try {
                    var i = {
                        reward: t.count,
                        pic: new cc.SpriteFrame(o),
                        name: t.name,
                        type: "activity"
                    };
                    Global.UIPopupManager.addPopup("PopupPanel/Panel_shareGroup_Reward", function (e) {
                        e.getComponent("shareGroup_RewardPanel").setData(i, n);
                    });
                } catch (e) {
                    cc.error("ActivityPanel ", "Error = " + e);
                }
            });
        }
        for (var o = ["activities1", "activities3"], i = 0; i < o.length; i++) this.initUI(o[i]);
    },
    initUI: function (e) {
        if (console.log("initUI", e), this._tasks) if ("activities1" == e) {
            var t = Global.utils.searchComp(this.activity1Content, "progressBar", cc.ProgressBar),
                n = Global.utils.searchComp(this.activity1Content, "progressNum", cc.Label),
                o = Global.utils.searchComp(this.activity1Content, "btnIcon", cc.Sprite),
                i = Global.utils.searchNode(this.activity1Content, "videoBtn"),
                a = Global.utils.searchNode(this.activity1Content, "Normalline"),
                r = Global.utils.searchComp(this.activity1Content, "labDuration", cc.Label);
            i.active = !1, a.active = !1;
            var s = o.node.parent;
            s.off("touchend", this.onBtnClicked, this);
            var l = Global.ActivityTaskModel.getActivitiesConfArrByIndex(1);
            if (!l || l.length <= 0) return void console.error("error: confArr == ", l);
            var c = l[0];
            if (null == c) return void console.log("error: null == activities_1");
            r && (r.string = Global.ActivityTaskModel.getActivityDurationByIndex(1)), this.activity_1 = null;
            for (var d = 0; d < this._tasks.length; d++) if (c.kindId == this._tasks[d][1]) {
                this.activity_1 = this._tasks[d];
                break;
            }
            switch (t.progress = this.activity_1[2] / c.progressMax, n.string = "（" + this.activity_1[2] + "/" + c.progressMax + "）",
                this.activity_1[3]) {
                case 0:
                    i.active = !1, a.active = !1, s.active = !0, Global.spriteAtlas.setSpriteFrame(o, "Texture/task", "RW_quwancheng"),
                        s.on("touchend", this.onBtnClicked, this);
                    break;

                case 1:
                    i.active = !0, a.active = !0, s.active = !1, Global.spriteAtlas.setSpriteFrame(o, "Texture/shareGetDiamond", "CG_AnNiuZi_3");
                    break;

                case 2:
                    i.active = !1, a.active = !1, s.active = !0, Global.spriteAtlas.setSpriteFrame(o, "Texture/task", "RW_yiwcheng02");
                    var u = s.getComponent(cc.Sprite);
                    Global.spriteAtlas.setSpriteFrame(u, "Texture/shareGetDiamond", "TY_ICON4");
            }
            var h = this.activity_1[0], f = this.activity_1[1], p = this.activity_1;
            this.targetClickData.activity1_content = {
                data: p,
                taskId: h,
                kindId: f
            };
        } else if ("activities2" == e) ; else if ("activities3" == e) {
            var g = Global.utils.searchNode(this.activity3Content, "layoutProgressBar"),
                b = Global.utils.searchComp(this.activity3Content, "btnIcon", cc.Sprite),
                m = Global.utils.searchComp(this.activity3Content, "labTaskDes", cc.Label),
                _ = Global.utils.searchComp(this.activity3Content, "labReward", cc.Label),
                y = Global.utils.searchComp(this.activity3Content, "labProgress", cc.Label),
                S = Global.utils.searchNode(this.activity3Content, "layoutContent"),
                I = Global.utils.searchNode(this.activity3Content, "gifts"), v = b.node.parent,
                C = Global.utils.searchNode(this.activity3Content, "videoBtn"),
                G = Global.utils.searchNode(this.activity3Content, "Normalline"),
                T = Global.utils.searchComp(this.activity3Content, "labDuration", cc.Label);
            C.active = !1, G.active = !1;
            var w = function () {
                var e = N[k].progressMax, t = this.activity_3[x][2];
                try {
                    0 == k ? (t = this.activity_3[x][2], e = N[k].progressMax) : 1 == k ? (t = this.activity_3[x][2] - N[k - 1].progressMax,
                        e = N[k].progressMax - N[k - 1].progressMax) : 2 == k && (t = this.activity_3[x][2] - N[k - 1].progressMax - N[k - 2].progressMax,
                        e = N[k].progressMax - N[k - 1].progressMax - N[k - 2].progressMax), U[k] = t / e;
                } catch (n) {
                    console.error("progress e :", n), U[k] = t / e;
                }
            }.bind(this), E = function () {
                w();
                var e = this.activity_3[x][3];
                m.string = D, _.string = "x" + L;
                var t = N[N.length - 1];
                y.string = "(" + this.activity_3[x][2] + "/" + t.progressMax + ")";
                for (var n = 0; n < N.length; n++) {
                    var o = I.children[n], i = g.children[n].getComponent(cc.ProgressBar),
                        a = S.children[n].getComponent(cc.Label), r = N[n].kindId, s = N[n].progressMax,
                        l = N[n].reward;
                    i.progress = U[n], a.string = "对局" + s + "局，获得" + l + "金币";
                    var c = this.activity_3[r][3];
                    this._setGiftLook(o, c, s);
                }
                this._setBottomUI(C, G, v, b, R, e);
            }.bind(this), A = Global.ActivityTaskModel.getActivitiesConfArrByIndex(3);
            if (!A || A.length <= 0) return void console.error("error: confArr == ", A);
            T && (T.string = Global.ActivityTaskModel.getActivityDurationByIndex(3));
            var N = A;
            this.activity_3 = {};
            for (var M = 0; M < this._tasks.length; M++) for (var P = 0; P < N.length; P++) if (N[P].kindId == this._tasks[M][1]) {
                this.activity_3[N[P].kindId] = this._tasks[M];
                break;
            }
            for (var R = !1, k = 0, x = N[k].kindId, D = N[k].activity_desc, L = N[k].reward, O = -1, U = [], B = 0; B < N.length; B++) U.push(0);
            var F = function (e) {
                e || (x = N[++k].kindId);
                var t = 0;
                D = N[t = -1 != O ? O : k].activity_desc, L = N[t].reward;
            };
            if (x && 0 == this.activity_3[x][3]) ; else if (U[k] = 1, x && 1 == this.activity_3[x][3] && (R = !0,
            -1 == O && (O = k)), F(), x && 0 == this.activity_3[x][3]) -1 == O && (O = k); else if (U[k] = 1,
            x && 1 == this.activity_3[x][3] && (R = !0, -1 == O && (O = k)), F(), x && 0 == this.activity_3[x][3]) -1 == O && (O = k); else {
                U[k] = 1, x && 1 == this.activity_3[x][3] && (R = !0, -1 == O && (O = k));
                F(!0);
            }
            E(), -1 == O && (O = 0), console.log(" taskArr currentClickIndex :", N, O);
            var H = N[O].taskId, W = N[O].kindId, j = this.activity_3[W];
            this.targetClickData.activity3_content = {
                data: j,
                taskId: H,
                kindId: W
            };
        }
    },
    _setBottomUI: function (e, t, n, o, i, a) {
        switch (e.active = i, t.active = i, n.active = !i, n.off("touchend", this.onBtnClicked, this),
            n.on("touchend", this.onBtnClicked, this), a) {
            case 0:
                Global.spriteAtlas.setSpriteFrame(o, "Texture/task", "RW_quwancheng");
                break;

            case 1:
                Global.spriteAtlas.setSpriteFrame(o, "Texture/shareGetDiamond", "CG_AnNiuZi_3");
                break;

            case 2:
                Global.spriteAtlas.setSpriteFrame(o, "Texture/task", "RW_yiwcheng02");
                var r = n.getComponent(cc.Sprite);
                Global.spriteAtlas.setSpriteFrame(r, "Texture/shareGetDiamond", "TY_ICON4");
        }
    },
    _setGiftLook: function (e, t, n) {
        var o = function () {
            var e = new cc.Sequence(cc.rotateTo(.2, 12), cc.rotateTo(.2, 0), cc.rotateTo(.2, -12), cc.rotateTo(.2, 0), cc.rotateTo(.2, 12), cc.rotateTo(.2, 0), cc.rotateTo(.2, -12), cc.rotateTo(.8, 0).easing(cc.easeBackOut(3)), cc.delayTime(.5));
            return new cc.RepeatForever(e);
        }.bind(this);
        e.off("touchend", this.onBtnClicked, this);
        var i = e.getChildByName("sp").getComponent(cc.Sprite),
            a = e.getChildByName("contentText").getComponent(cc.Label);
        switch (a.string = n + "局", i.node.stopAllActions(), i.node.rotation = 0, t) {
            case 0:
                Global.spriteAtlas.setSpriteFrame(i, "Texture/huodong", "lh02");
                break;

            case 1:
                i.node.runAction(o()), e.on("touchend", this.onBtnClicked, this);
                break;

            case 2:
                Global.spriteAtlas.setSpriteFrame(i, "Texture/huodong", "lh"), a.node.setPositionX(-10);
        }
    },
    onBtnVideoClicked: function () {
        var e = "";
        this.activity1Content.active ? e = "activity1_content" : this.activity3Content.active && (e = "activity3_content");
        var t = this.targetClickData[e], n = t.kindId, i = t.taskId, a = this;
        Global.utils.showViewAd(Global.config.adTaskUnitId, function () {
            a.scheduleOnce(function () {
                a.gotoReward(i, n, 1);
            }, .2);
        }, function () {
            Global.utils.showNodeCommon("您在播放中途退出，无法领取游戏奖励", 3);
        }), o.playEffect("select");
    },
    onBtnClicked: function () {
        o.playEffect("select");
        var e = function (e) {
            if (console.log("onBtnClicked obj:", e), e) {
                var t = e.kindId, n = e.taskId;
                switch (e.data[3]) {
                    case 0:
                        this.gotoComplete();
                        break;

                    case 1:
                        this.gotoReward(n, t);
                }
            }
        }.bind(this);
        if (this.targetClickData) {
            var t = "";
            this.activity1Content.active ? (t = "activity1_content", this.defaultPlayMode = Global.config.playModel.XUELIU,
                this.defaultGameId = Global.config.GAMEID_NORMAL) : this.activity3Content.active && (t = "activity3_content",
                this.defaultPlayMode = Global.config.playModel.ERRENQUICK, this.defaultGameId = Global.config.GAMEID_ERRENQUICK),
                e(this.targetClickData[t]);
        } else if (this.activity2Content.active && this.activity_2) switch (this.activity_2[3]) {
            case 0:
                var n = cc.instantiate(this.createPrefab);
                n ? n.parent = this.node : console.log("createPrefab failed!");
                break;

            case 1:
                this.gotoReward(this.activity_2[0], this.activity_2[1]);
        }
    },
    gotoReward: function (e, t, n) {
        var o = {
            taskId: e,
            kindId: t,
            type: n || 0
        };
        Global.msgHandler.requestGetTaskReward(o);
    },
    gotoComplete: function () {
        var e = {
            playMode: this.defaultPlayMode,
            gameId: this.defaultGameId,
            type: this.defaultPlayMode
        };
        Global.notificationCenter.emit(Global.eventName.MSG_DO_QUICK_START, e);
    },
    OnPanelClose: function () {
        o.playEffect("select"), this.closePanel();
    },
    onDestroyHook: function () {
        Global.notificationCenter.emit(Global.eventName.MSG_SET_UNDO_RECEIVE, !1);
    }
})
